package cn.mumzone.pos.model.source;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.source.AbsRepository;
import cn.mumzone.basic.util.RxUtil;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.api.ApiRequest;
import cn.mumzone.pos.model.ScheduleEmployeeEntity;
import cn.mumzone.pos.model.ScheduleResultEntity;
import retrofit2.Response;

/**
 * Created by qiuzhenhuan on 2019/7/10.
 */


public class ScheduleRepository extends AbsRepository {


    /**
     * 获取排班状态
     *
     * @param callBack
     */
    public void getScheduleStatus(ApiCallBack<List<String>> callBack) {
        ApiRequest.getScheduleService()
                .getScheduleStatus()
                .compose(RxUtil.<Response<BaseResponse<List<String>>>>applySchedulers())
                .subscribe(callBack);
    }

    /**
     * 排班详情
     *
     * @param callBack
     * @param scheduleDay
     */
    public void getScheduleInfo(ApiCallBack<ScheduleResultEntity> callBack, String scheduleDay) {
        ApiRequest.getScheduleService()
                .getScheduleInfo(scheduleDay)
                .compose(RxUtil.<Response<BaseResponse<ScheduleResultEntity>>>applySchedulers())
                .subscribe(callBack);
    }


    /**
     * 添加人员到某个班次
     *
     * @param callBack
     * @param scheduleId
     * @param scheduleName
     * @param scheduleDay
     * @param employeeId
     */
    public void addEmployeeToSchedule(String scheduleId,
                                      String scheduleName, String scheduleDay, String employeeId,ApiCallBack<String> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("orgId", AppConstants.TEMP_SHOP_ID);
        requestMap.put("orgName", AppConstants.TEMP_SHOP_NAME);
        requestMap.put("scheduleId",scheduleId);
        requestMap.put("scheduleName",scheduleName);
        requestMap.put("scheduleDay",scheduleDay);
        requestMap.put("employeeId",employeeId);
        ApiRequest.getScheduleService()
                .addEmployeeToSchedule(requestMap)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    /**
     * 移除班次某人员
     *
     * @param callBack
     * @param scheduleDay
     * @param scheduleId
     * @param employeeId
     * @param startTime
     * @param endTime
     */
    public void removeEmployeeFromSchedule(ApiCallBack<String> callBack,String scheduleDay,
                                           String scheduleId, String employeeId, String startTime, String endTime) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("scheduleDay",scheduleDay);
        requestMap.put("scheduleId",scheduleId);
        requestMap.put("employeeId",employeeId);
        requestMap.put("startTime",startTime);
        requestMap.put("endTime",endTime);
        ApiRequest.getScheduleService()
                .removeEmployeeFromSchedule(requestMap)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    /**
     * 换班
     *
     * @param callBack
     * @param fromEmployee
     * @param toEmployee
     */
    public void changeShift(ApiCallBack<String> callBack, ScheduleEmployeeEntity fromEmployee, ScheduleEmployeeEntity toEmployee) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("fromEmployee", JSON.toJSONString(fromEmployee));
        requestMap.put("toEmployee",JSON.toJSONString(toEmployee));
        requestMap.put("orgId", AppConstants.TEMP_SHOP_ID);
        requestMap.put("orgName", AppConstants.TEMP_SHOP_NAME);
        ApiRequest.getScheduleService()
                .changeShift(requestMap)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }


    /**
     * 校验人员是否能换班
     *
     * @param callBack
     * @param scheduleDay
     * @param scheduleId
     * @param employeeId
     * @param startTime
     * @param endTime
     */
    public void checkEmployeeDoChangeShift(ApiCallBack<String> callBack, String scheduleDay, String scheduleId,
                                           String employeeId, String startTime, String endTime) {
        ApiRequest.getScheduleService()
                .getSubscribeScheduleEmployee(scheduleDay, scheduleId, employeeId,startTime,endTime)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

}
